var express = require("express")
var fs = require("fs")
var app = express()
app.use(express.static("public"))
app.listen(5000, function(){
    console.log("server is running, please access http://localhost:5000")
})
var hpm = require('http-proxy-middleware')
var proxy = hpm.createProxyMiddleware
app.use("/binge/news", proxy({
    // 目标数据所在的跨域地址
    target: "https://api.aijiehu.cn/gateway-ext/news",
    changeOrigin: true,
    pathRewrite: {
        '^/binge': ""
    }
}))
app.get("/binge/public", function(req,res){
    if(req.query.type == "nav"){
        fs.readFile("./public/nav.json", (err,data)=>{
            res.send(data.toString())
        })
    }else if(req.query.type == "menu"){
        fs.readFile("./public/fixed_menu.json", (err,data)=>{
            res.send(data.toString())
        })
    }else if(req.query.type == "footer"){
        fs.readFile("./public/footer.json", (err,data)=>{
            res.send(data.toString())
        })
    }else{
        res.send({message: '参数错误'})
    }
})
app.get("/binge/home", function(req,res){
    if(req.query.type == "banner"){
        fs.readFile("./public/banner.json", (err,data)=>{
            res.send(data.toString())
        })
    }else if(req.query.type == "main"){
        fs.readFile("./public/home_data.json", (err,data)=>{
            res.send(data.toString())
        })
    }else{
        res.send({message: '参数错误'})
    }
})
app.get("/binge/about", function(req,res){
    fs.readFile("./public/about.json", (err,data)=>{
        res.send(data.toString())
    })
})
app.get("/binge/newsPage", function(req,res){
    fs.readFile("./public/news.json", (err,data)=>{
        res.send(data.toString())
    })
})
app.get("/binge/newsList", function(req,res){
    fs.readFile("./public/newsList.json", (err,data)=>{
        if(req.query.page == 1){
            res.json(JSON.parse(data.toString()).slice(0,10))
        }else if(req.query.page == 2){
            res.json(JSON.parse(data.toString()).slice(10,15))
        }else{
            res.send({message: '参数错误'})
        }
    })
})
app.get("/binge/service/items", function(req,res){
    fs.readFile("./public/service.json", (err,data)=>{
        res.send(data.toString())
    })
})
app.get("/binge/service/station", function(req,res){
    fs.readFile("./public/station.json", (err,data)=>{
        res.send(data.toString())
    })
})
app.get("/binge/joinUs", function(req,res){
    fs.readFile("./public/joinus.json", (err,data)=>{
        res.send(data.toString())
    })
})